<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>33题求计算体重</title>
    <style>
        div {
            width: 100%;
            
        }
        span {
            display: block;
            width: 100%;
        }
    </style>
    <script>
        var height = 0;
        var weight = 0;
        function getWeight(flag) {
            if (!height) {
                height = prompt(flag ? '请输入正确身高(单位cm且大于108)' : '请输入您的身高(单位cm且大于108)');
                flag = false;
            }
            if (!/^[0-9]*$/i.test(height)) {
                height = 0;
                getWeight(true);
                return;
            }
            if (parseInt(height) <= 108) {
                height = 0;
                getWeight(true);
                return;
            }

            if (!weight) {
                weight = prompt(flag ? '请输入正确体重(单位kg且大于0)' : '请输入您的体重(单位kg且大于0)');
                flag = false;
            }
            if (!/^[0-9]*$/i.test(height)) {
                weight = 0;
                getWeight(true);
                return;
            }
            if (parseInt(weight) <= 0) {
                weight = 0;
                getWeight(true);
                return;
            }
            //计算理想体重
            let ideal_weight = (parseInt(height) - 108) * 2;

            let span = document.getElementById('weight');
            span.innerHTML = `您的身高：${height}cm，体重：${weight}kg，理想体重范围：${ideal_weight - 5 < 1 ? 1 : ideal_weight - 5}kg~${ideal_weight + 5}kg<br>`
            // 理想体重
            if (parseInt(weight) > ideal_weight + 5) {
                span.innerHTML += '您当前体重属于超重';
            } else if (parseInt(weight) < ideal_weight - 5) {
                span.innerHTML += '您当前体重属于偏瘦';
            } else {
                span.innerHTML += '您当前体重在理想范围';
            }
        }
    </script>
</head>
<body>
    <div>
        请点击按钮输入您的身高(单位cm且大于108)和体重(kg，且大于0)<input type="button" value="输入" onclick="getWeight()">
        <span id="weight"></span>
    </div>
</body>
</html>